<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="addCate">添加分类</el-button>
      <tree-grid 
      :data="goodsList" 
      :columns="columns"
      :expand-type="false"
      :selection-type="false"
      :show-index="true"
      index-text="#"
      border
      >
        <!-- 是否有效模板 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" style="color:blue" v-if="!scope.row.cat_deleted"></i>
          <i class="el-icon-error" style="color:blue" v-else></i>
        </template>
      <!-- 排序模板 -->
        <template slot="sort" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level=='0'">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level=='1'" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <!-- 操作模板 -->
        <template slot="opt" slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-grid>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <!-- 添加分类对话框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="AddCateDialogVisible"
        width="40%"
        @close="AddCateDialogVisibleClose">
        <el-form :model="addCateRuleForm" :rules="addCateRules" ref="addCateFormRef" label-width="100px">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateRuleForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类" >
            <!-- 级联选择器 -->
              <el-cascader
                v-model="selectOpt"
                :options="parentCate"
                :props="propsObj"
                change-on-select
                clearable
                @change="handleChange">
              </el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="AddCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="subAddCate">确 定</el-button>
        </span>
      </el-dialog>
      
    </el-card>
  </div>
</template>

<script>
export default {

  data () {
    return {
      queryInfo:{
        type:3,
        pagenum:1,
        pagesize:8
      },
      goodsList:[],
      total:0,
      columns:[
        {
          label:'商品分类',
          prop:'cat_name'
        },
        {
          label:'是否有效',
          type:'template',
          template:'isok'
        },
        {
          label:'排序',
          type:'template',
          template:'sort'
        },
        {
          label:'操作',
          type:'template',
          template:'opt'
        }
      ],
      // 添加分类对话框现实与隐藏
      AddCateDialogVisible:false,
      // 添加分类数据
      addCateRuleForm:{
        cat_pid:0,
        cat_name:'',
        cat_level:0
      },
       // 添加分类规则
      addCateRules:{
       cat_name:[
        { required: true, message: '请输入分类名称', trigger: 'blur' },
        { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
       ]
      },
      // 父级分类数据
      parentCate:[],
      // 选择好之后的id
      selectOpt:[],
      propsObj:{ 
        expandTrigger: 'hover',
        label:'cat_name',
        value:'cat_id'
       }
     
    }
  },

  methods: {
    async getCateList() {
      const {data:res} = await this.$http.get('categories',{
        params:this.queryInfo// get请求参数是对象的话用params传参
      })
      // console.log(res)
      this.goodsList = res.data.result
      this.total = res.data.total
      // console.log(this.goodsList)
    },
    // 分页
    handleSizeChange(val) {
      // console.log(val)
      this.queryInfo.pagesize = val
      this.getCateList()
    },
    handleCurrentChange(val) {
      // console.log(val)
      this.queryInfo.pagenum = val
      this.getCateList()
    },
    // 添加分类
    async addCate() {
      // 获取第二层的分类数据
      const {data:res} = await this.$http.get('categories',{
        params:{type:2 }// 获取第二层的分类列表数据
      })
      // console.log(res)
      this.parentCate = res.data
      // console.log(this.parentCate)
      this.AddCateDialogVisible = true
    },
    // 父级分类 
      handleChange() {
        console.log(this.selectOpt)
        if(this.selectOpt.length > 0) {
          this.addCateRuleForm.cat_level = this.selectOpt.length
          this.addCateRuleForm.cat_pid = this.selectOpt[this.selectOpt.length-1]
        } else {
          this.addCateRuleForm.cat_level = 0
          this.addCateRuleForm.cat_pid = 0
        }
        // console.log(this.addCateRuleForm)
      },
      // 关闭对话框  清空数据 重置数据
      AddCateDialogVisibleClose() {
        this.$refs.addCateFormRef.resetFields()// 表单重置 移除校验结果
        this.selectOpt= []  //父级分类数据清空
        console.log(this.addCateRuleForm)
        this.addCateRuleForm.cat_level = 0 // 清空 addCateRuleForm中数据
        this.addCateRuleForm.cat_pid = 0
      },
      // 点击确定添加分类发送请求
      subAddCate() {
        // 表单预校验
        this.$refs.addCateFormRef.validate(async flag => {
          if(!flag) return this.$message.error('服务器出错')
          this.$message.success('创建成功')
          var {data:res} = await this.$http.post('categories',this.addCateRuleForm)
          // console.log(res)
          this.getCateList()
          this.AddCateDialogVisible = false
        })
      }
  },

  created () {
    this.getCateList()
  }
}
</script>

<style lang='css' scoped>
  .zk-table {
    margin: 20px 0;
  }
</style>
